package com.hong.service_product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hong.fy_common_commomutil.common.result.Result;
import com.hong.fy_model.model.product.ProductEntity;

import com.hong.fy_model.vo.productVO.ProductAndModelVo;
import com.hong.service_product.service.ProductModelService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;



import com.hong.service_product.service.ProductService;



/**
 * 
 *
 * @author wqh
 * @email 1601709391@qq.com
 * @date 2022-06-15 20:37:30
 */
@Api(tags = "商品")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductModelService productModelService;


    @ApiOperation(value = "新增商品接口")
    @PostMapping("/add")
    public Result addProduct(@RequestBody ProductEntity product){
        Boolean flag = productService.addProduct(product);
        if(flag) {
            return Result.ok("新增成功");
        }
        return Result.fail("新增失败");


    }

    @ApiOperation(value = "根据商品ID逻辑删除商品")
    @DeleteMapping("/delete/{id}")
    public Result deleteProduct(@PathVariable @ApiParam(value = "商品" + "id") Integer id){
        Boolean flag = productService.deleteProduct(id);
        if(flag) {
            return Result.ok("删除成功");
        }

        return Result.fail("删除失败");


    }

    @ApiOperation(value = "更新商品")
    @PutMapping("/update")
    public Result updateProduct(@RequestBody ProductEntity product){
        Boolean flag = productService.updateProduct(product);
        if(flag) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }

    @ApiOperation(value = "查询分页数据")
    @GetMapping("/listByPage")
    public Result findListByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        IPage<ProductEntity> list = productService.findListByPage(pageNum, limit);
        return Result.ok(list);
    }

    @ApiOperation(value = "根据商品ID查询商品")
    @GetMapping("/getById/{id}")
    public Result getProductById(@PathVariable @ApiParam(value = "id") Integer id){
        ProductEntity product = productService.getProductById(id);
        if (product != null) {
            return Result.ok(product);
        }
        return Result.fail("查询失败");
    }

    @ApiOperation(value = "根据商品ID查询商品的模型地址")
    @GetMapping("/getModelUrlById/{id}")
    public Result getModelUrlById(@PathVariable @ApiParam(value = "id") Integer id){
        ProductEntity product = productService.getById(id);
        if (product == null) {
            return Result.fail("商品不存在");
        }
        ProductAndModelVo res = new ProductAndModelVo();
        res.setProductId(product.getProductId());
        res.setName(product.getName());
        //根据模型id查询表
        res.setProductModel(productModelService.getById(product.getModelId()));

        return Result.ok(res);
    }


    @ApiOperation(value = "根据商品类别分页ID查询商品")
    @GetMapping("/getByCategoryId/{categoryId}")
    public Result getByCategoryId(@PathVariable @ApiParam(value = "商品类别的id") Integer categoryId,
                                  @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        IPage<ProductEntity> list = productService.getByCategoryId(categoryId, pageNum, limit);
        return Result.ok(list);
    }

    @ApiOperation(value = "上传商品图片，还涉及oss模块，so未写完")
    @PostMapping("/upload")
    public Result upload(@RequestBody ProductEntity product){
        return null;
    }


}
